package com.example.concurrency.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class SynvchronizedExample2 {


    // 修饰一个类  作用于当前类
    public void test1(int j){
        synchronized (SynvchronizedExample2.class){
            for (int i = 0; i < 10; i++) {
                log.info("test1 {} - {}",j,i);
            }
        }
    }

    // 修饰一个静态方法
    public static synchronized void test2(int j){
        for (int i = 0; i < 10; i++) {
            log.info("test2  {} - {}",j,i);
        }
    }

    public static void main(String[] args) {

        SynvchronizedExample2 synvchronizedExample1 = new SynvchronizedExample2();
        SynvchronizedExample2 synvchronizedExample2 = new SynvchronizedExample2();

        // 声明线程池
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(()->{
            synvchronizedExample1.test1(1);
        });
        executorService.execute(()->{
//            synvchronizedExample1.test2();
            synvchronizedExample2.test1(2);
        });

    }

}
